package com.example.interfacecall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.interfacecall.domain.BaoHanNoticeInDto;
import com.example.interfacecall.domain.Book;
import com.example.interfacecall.domain.Result;
import com.example.interfacecall.exception.BusinessException;
import com.example.interfacecall.exception.MyException;
import com.example.interfacecall.util.HttpUtils;
import com.example.interfacecall.util.ResultWrapper;
import com.example.interfacecall.util.ResultWrapperMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class BaoHanNoticeController {
    @Value("${interface.url}")
    private String baohannoticeUrl;

    @RequestMapping(value = "/baohannoticePage")
    public ModelAndView  baohannoticePage(Model model)
    {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("baohannotice");
        return mv;
    }

    @RequestMapping(value = "/baohannoticecall")
    public ResultWrapper<Result> baohannoticeCall(@Valid @RequestBody @ModelAttribute(value = "baohannoticeInDto") BaoHanNoticeInDto baoHanNoticeInDto)  {

        try{
            String interfaceName = "baohannotice";
            String url = baohannoticeUrl +"/"+ interfaceName;
            String param = JSON.toJSONString(baoHanNoticeInDto);
            System.out.println(param);
            String resultOut = HttpUtils.sendPost(url, param);
            Result result = JSON.parseObject(resultOut, Result.class);
            return ResultWrapperMapper.ok(result);
        } catch (BusinessException e){
            return ResultWrapperMapper.wrap(e);
        }

    }
}
